//
//  MustViewController.m
//  心水宝典
//
//  Created by zr on 2018/8/27.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MustViewController.h"
#import "FunTuiSuanHeaderView.h"
#import "FunTuiSuanTableViewCell.h"
#import "LifeTableViewCell.h"
#import "ClubTableViewCell.h"
#import "WeiteTieZiViewController.h"
#import "FunModel.h"
#import "AskListModel.h"
#import "FunInfoViewController.h"
#import "LifeInfoViewController.h"
#import "ClubInfoViewController.h"

@interface MustViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * oneTableView;
@property (nonatomic, weak)UITableView * twoTableView;
@property (nonatomic, strong)UIButton * oneButton;
@property (nonatomic, strong)UIButton * twoButton;
@property (nonatomic, strong)UIButton * threeButton;
@property (nonatomic, weak)UIButton * pushButton;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, copy)NSString * type;

@property (nonatomic, strong)NSMutableArray <FunModel *> * funModelArray;
@property (nonatomic, strong)NSMutableArray <FunModel *> * lifeModelArray;
@property (nonatomic, strong)NSMutableArray <AskListModel *> * clubModelArray;
@property (nonatomic, strong)NSArray <FunModel *> * funFirstArray;

@end

@implementation MustViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    self.type = @"fun";
    self.funModelArray = [NSMutableArray array];
    self.lifeModelArray = [NSMutableArray array];
    self.clubModelArray = [NSMutableArray array];
    self.funFirstArray = [NSArray array];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToFunInfo:) name:@"pushToFunInfo" object:nil];
    
    [self loadDataWithType:self.type];
    [self setupUI];
}


- (void)loadDataWithType:(NSString *)type{
    
    self.funFirstArray = nil;
    [self.funModelArray removeAllObjects];
    [self.lifeModelArray removeAllObjects];
    [self.clubModelArray removeAllObjects];
    
    if ([type isEqualToString:@"life"]) {
        
        [[NetworkTool sharedTools] GET:@"/api/article/lists?project_id=1&pid=10" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {

            NSArray *array = [NSArray yy_modelArrayWithClass:[FunModel class] json:responseObject[@"data"]];

            [self.lifeModelArray addObjectsFromArray:array];
            [self.twoTableView reloadData];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            
            NSLog(@"errror===%@",error);
        }];
    }else if ([type isEqualToString:@"fun"]){
        
        [[NetworkTool sharedTools] GET:@"/api/article/lists?project_id=1&pid=9" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            NSArray *array = [NSArray yy_modelArrayWithClass:[FunModel class] json:responseObject[@"data"]];
            
            [self.funModelArray addObjectsFromArray:array];
            self.funFirstArray = [self.funModelArray subarrayWithRange:NSMakeRange(0, 3)];
            [self.oneTableView reloadData];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            
            NSLog(@"errror===%@",error);
        }];
    }else if ([type isEqualToString:@"club"]){
        
        [[NetworkTool sharedTools] GET:@"/api/circle?project_id=1&pid=12" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            NSArray *array = [NSArray yy_modelArrayWithClass:[AskListModel class] json:responseObject[@"data"]];
            [self.clubModelArray addObjectsFromArray:array];
            [self.twoTableView reloadData];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            
            NSLog(@"errror===%@",error);
        }];
    }
}

- (void)setupUI{
    
    self.oneButton = [self setupButtonWith:@"趣味测算"];
    self.oneButton.selected = YES;
    
    [self.view addSubview:self.oneButton];
    
    self.twoButton = [self setupButtonWith:@"命理专栏"];
    [self.view addSubview:self.twoButton];
    self.threeButton = [self setupButtonWith:@"社区动态"];
    [self.view addSubview:self.threeButton];
    
    self.oneLineView = [self setupLineView];
    self.oneLineView.hidden = NO;
    
    [self.view addSubview:self.oneLineView];
    
    self.twoLineView = [self setupLineView];
    [self.view addSubview:self.twoLineView];
    self.threeLineView = [self setupLineView];
    [self.view addSubview:self.threeLineView];
    
    
    UIButton * pushButton = [UIButton new];
    
    [pushButton setImage:ImageNamed(@"xie") forState:UIControlStateNormal];
    [pushButton addTarget:self action:@selector(clickPushButton) forControlEvents:UIControlEventTouchUpInside];
    pushButton.hidden = YES;
    self.pushButton = pushButton;
    
    [self.view addSubview:self.pushButton];
    
    
    UITableView * oneTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 55 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT - 100 * SCREENH6) style:UITableViewStyleGrouped];
    oneTableView.delegate = self;
    oneTableView.dataSource = self;
    oneTableView.backgroundColor = [UIColor whiteColor];
//    oneTableView.rowHeight = 85 * SCREENH6;
    oneTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.oneTableView = oneTableView;
    
    [self.oneTableView registerClass:[FunTuiSuanTableViewCell class] forCellReuseIdentifier:@"FunTuiSuanTableViewCell"];
    [self.oneTableView registerClass:[FunTuiSuanHeaderView class] forHeaderFooterViewReuseIdentifier:@"FunTuiSuanHeaderView"];
    
    [self.view addSubview:self.oneTableView];
    
    
    UITableView * twoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 55 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT - 100 * SCREENH6) style:UITableViewStyleGrouped];
    twoTableView.delegate = self;
    twoTableView.dataSource = self;
    twoTableView.backgroundColor = [UIColor whiteColor];
//    twoTableView.rowHeight = 85 * SCREENH6;
    twoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    twoTableView.hidden = YES;
    
    self.twoTableView = twoTableView;
    
    [self.twoTableView registerClass:[LifeTableViewCell class] forCellReuseIdentifier:@"LifeTableViewCell"];
    [self.twoTableView registerClass:[ClubTableViewCell class] forCellReuseIdentifier:@"ClubTableViewCell"];
    
    [self.view addSubview:self.twoTableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([self.type isEqualToString:@"fun"]) {
        
        return self.funModelArray.count - 3;
    }else if([self.type isEqualToString:@"life"]){
        
        return self.lifeModelArray.count;
    }else{
        
        return self.clubModelArray.count;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if ([self.type isEqualToString:@"life"]) {
        
        LifeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LifeTableViewCell" forIndexPath:indexPath];
        
        cell.model = self.lifeModelArray[indexPath.row];
        
        return cell;
    }else if ([self.type isEqualToString:@"fun"]){
        
        FunTuiSuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FunTuiSuanTableViewCell" forIndexPath:indexPath];
        
        cell.model = self.funModelArray[indexPath.row + 3];
        
        return cell;
    }else{
        
        ClubTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClubTableViewCell" forIndexPath:indexPath];
        
        cell.model = self.clubModelArray[indexPath.row];
        
        return cell;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.type isEqualToString:@"life"]) {
        
        return 96 * SCREENH6;
    }else if ([self.type isEqualToString:@"fun"]){
        
        return 85 * SCREENH6;
    }else{
        
        return 250 * SCREENH6;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    if ([self.type isEqualToString:@"fun"]) {
        
        return 380 * SCREENH6;
    }else{
        
        return 0;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if ([self.type isEqualToString:@"fun"]) {
        
        FunTuiSuanHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"FunTuiSuanHeaderView"];
        
        header.funFirstArray = self.funFirstArray;
        
        return header;
    }else{
        
        
        return nil;
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    if ([self.type isEqualToString:@"fun"]) {
        
        FunModel * model = self.funModelArray[indexPath.row + 3];
        
        FunInfoViewController * infoVC = [FunInfoViewController new];
        
        infoVC.ID = model.id;
        
        [self.navigationController pushViewController:infoVC animated:YES];
    }else if ([self.type isEqualToString:@"life"]){
        
        FunModel * model = self.lifeModelArray[indexPath.row];
        
        LifeInfoViewController * infoVC = [LifeInfoViewController new];
        
        infoVC.model = model;
        
        [self.navigationController pushViewController:infoVC animated:YES];
    }else if ([self.type isEqualToString:@"club"]){
        
        AskListModel * model = self.clubModelArray[indexPath.row];
        
        ClubInfoViewController * infoVC = [ClubInfoViewController new];
        
        infoVC.ID = model.id;
        
        [self.navigationController pushViewController:infoVC animated:YES];
    }
    
    
    SHOW_TAB;
}

- (void)pushToFunInfo:(NSNotification *)noti{
    
    int row = [noti.userInfo[@"type"] intValue];
    
    FunModel * model = self.funModelArray[row];
    
    HIDDEN_TAB;
    
    FunInfoViewController * infoVC = [FunInfoViewController new];
    
    infoVC.ID = model.id;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
    SHOW_TAB;
}

- (void)viewDidLayoutSubviews{
    
    [self.oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(30 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(65 * SCREENH6);
        make.width.mas_equalTo(75 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];
    
    [self.twoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(self.oneButton);
        make.leading.mas_equalTo(self.oneButton.mas_trailing);
    }];
    
    [self.threeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.mas_equalTo(self.twoButton);
        make.leading.mas_equalTo(self.twoButton.mas_trailing);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.oneButton.mas_bottom);
        make.height.mas_equalTo(3 * SCREENH6);
        make.width.mas_equalTo(70 * SCREENH6);
        make.centerX.mas_equalTo(self.oneButton);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.mas_equalTo(self.oneLineView);
        make.centerX.mas_equalTo(self.twoButton);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.mas_equalTo(self.twoLineView);
        make.centerX.mas_equalTo(self.threeButton);
    }];
    
    [self.pushButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.threeButton);
        make.width.mas_equalTo(21 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];
}


- (UIButton *)setupButtonWith:(NSString *)string{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:string forState:UIControlStateNormal];
    [button setTitleColor:ColorWithRGB(0x585858) forState:UIControlStateNormal];
    [button setTitleColor:ColorWithRGB(0xF52F4C) forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xF52F4C);
    lineView.hidden = YES;
    
    return lineView;
}


- (void)clickPushButton{
    
    HIDDEN_TAB;
    
    WeiteTieZiViewController * writeVC = [WeiteTieZiViewController new];
    
    [self.navigationController pushViewController:writeVC animated:YES];
    
    SHOW_TAB;
}


- (void)clickButton:(UIButton *)button{
    
    if (button == self.oneButton) {
        
        self.type = @"fun";
        self.oneTableView.hidden = NO;
        self.twoTableView.hidden = YES;
        self.pushButton.hidden = YES;
        [self.oneTableView reloadData];
        
        if (!self.oneButton.selected) {
            
            self.oneLineView.hidden = NO;
            self.oneButton.selected = YES;
            
            self.twoButton.selected = NO;
            self.twoLineView.hidden = YES;
            self.threeButton.selected = NO;
            self.threeLineView.hidden = YES;
        }
    }else if (button == self.twoButton){
        
         self.type = @"life";
        self.oneTableView.hidden = YES;
        self.twoTableView.hidden = NO;
        self.pushButton.hidden = YES;
        [self.twoTableView reloadData];
        
        if (!self.twoButton.selected) {
            
            self.twoLineView.hidden = NO;
            self.twoButton.selected = YES;
            
            self.oneButton.selected = NO;
            self.oneLineView.hidden = YES;
            self.threeButton.selected = NO;
            self.threeLineView.hidden = YES;
        }
    }else if (button == self.threeButton){
        
        self.type = @"club";
        
        self.oneTableView.hidden = YES;
        self.twoTableView.hidden = NO;
        self.pushButton.hidden = NO;
        [self.twoTableView reloadData];
        
        if (!self.threeButton.selected) {
            
            self.threeButton.selected = YES;
            self.threeLineView.hidden = NO;
            
            self.twoLineView.hidden = YES;
            self.twoButton.selected = NO;
            self.oneButton.selected = NO;
            self.oneLineView.hidden = YES;
        }
    }
    
    [self loadDataWithType:self.type];
}


@end
